package com.project.base.common;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SpecialSearchTypeParseUtil {

    private static final Logger LOGGER = Logger.getLogger(SpecialSearchTypeParseUtil.class);

    private static final String fileName = "/client-service-search-special-type.xls";

    /**
     * Map<接口名, Map<接口版本号, List<字段信息>>>   搜索特殊字段解析规则
     *
     * @return
     */
    public static Map<String, Map<String, List<Map<String, String>>>> parse() {
        // Map<接口名, Map<接口版本号, List<字段信息>>>   搜索特殊字段解析规则
        Map<String, Map<String, List<Map<String, String>>>> searchTypeMap = new HashMap<String, Map<String, List<Map<String, String>>>>();
        List<List<String[]>> result = ExcelUtil.parseExcel(fileName);
        if (result == null || result.isEmpty()) {
            LOGGER.error("搜索字段规则，解析结果为空，文件名：" + fileName);
            return searchTypeMap;
        }
        List<String[]> firstSheetDate = result.get(0);
        for (String[] aRowDate : firstSheetDate) {
            String methodName = aRowDate[0];
            Map<String, List<Map<String, String>>> aSearchRule = searchTypeMap.get(methodName);
            if (aSearchRule == null) {
                aSearchRule = new HashMap<String, List<Map<String, String>>>();
                searchTypeMap.put(methodName, aSearchRule);
            }
            String version = aRowDate[1];
            List<Map<String, String>> aVersionSearchRule = new ArrayList<Map<String, String>>();
            aSearchRule.put(version, aVersionSearchRule);
            for (int i = 2; i < aRowDate.length; i += 2) {
                if ((i + 1) >= aRowDate.length) {
                    break;
                }
                String paramCnName = aRowDate[i];
                String paramEnName = aRowDate[i + 1];
                if (StringUtils.isBlank(paramCnName) || StringUtils.isBlank(paramEnName)) {
                    LOGGER.error("Excel填写规则异常，CnName：" + paramCnName + "  EnName:" + paramEnName);
                    break;
                }
                Map<String, String> aParamRule = new HashMap<String, String>();
                aParamRule.put("cnName", paramCnName);
                aParamRule.put("enName", paramEnName);
                aVersionSearchRule.add(aParamRule);
            }
        }
        return searchTypeMap;
    }
}
